package com.jsbb.util;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Set;

/**
 * Http工具类
 *
 * @author jsbb
 */
public class HttpUtils {

    private static String utf_8 = "UTF-8";

    public static String get(String url) {
        HttpURLConnection connection = null;
        InputStream inputStream = null;
        BufferedReader bufferedReader = null;
        try {
            connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(15000);
            connection.setReadTimeout(15000);
            connection.connect();
            inputStream = connection.getInputStream();
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream, utf_8));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            connection.disconnect();
            closeResources(bufferedReader, inputStream, null);
        }
        return null;
    }

    public static String get(String url, Map<String, String> params) {
        String param = transformParam(params);
        return get(url + "?" + param);
    }

    public static String post(String url, Map<String, String> params) {
        String param = transformParam(params);
        HttpURLConnection connection = null;
        OutputStream outputStream = null;
        InputStream inputStream = null;
        BufferedReader bufferedReader = null;
        try {
            connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(15000);
            connection.setReadTimeout(15000);
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestProperty("charsert", utf_8);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            outputStream = connection.getOutputStream();
            outputStream.write(param.getBytes(utf_8));
            connection.connect();
            inputStream = connection.getInputStream();
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream, utf_8));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            connection.disconnect();
            closeResources(bufferedReader, inputStream, outputStream);
        }
        return null;
    }

    public static String post(String url, String json) {
        HttpURLConnection connection = null;
        OutputStream outputStream = null;
        InputStream inputStream = null;
        BufferedReader bufferedReader = null;
        try {
            connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(15000);
            connection.setReadTimeout(15000);
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            outputStream = connection.getOutputStream();
            outputStream.write(json.getBytes(utf_8));
            inputStream = connection.getInputStream();
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream, utf_8));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            connection.disconnect();
            closeResources(bufferedReader, inputStream, outputStream);
        }
        return null;
    }

    private static String transformParam(Map<String, String> params) {
        String result = null;
        StringBuilder sb = new StringBuilder();
        Set<String> keySet = params.keySet();
        for (String key : keySet) {
            sb.append(key).append("=").append(params.get(key)).append("&");
        }
        if (sb.length() > 1) {
            result = sb.substring(0, sb.length() - 1).toString();
        }
        return result;
    }

    private static void closeResources(BufferedReader bufferedReader, InputStream inputStream, OutputStream outputStream) {
        try {
            if (bufferedReader != null) {
                bufferedReader.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
